package com.itfreer.ui.advanced;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.itfreer.ui.R;
import com.itfreer.ui.advanced.base.PageViewAdapter;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 定义广告对象
 */
public class AdvertView extends RelativeLayout {

    private ViewPager advPager;
    private LinearLayout group;

    /**
     * 图片地址集
     */
    private ArrayList<Integer> images;

    private ImageView[] imageViews = null;
    private ImageView imageView = null;
    private AtomicInteger what = new AtomicInteger(0);
    private boolean isContinue = true;

    /**
     * 设置图片集
     * @param images
     */
    public void setImages(ArrayList<Integer> images){
        this.images = images;
        initView(getContext());
    }

    public AdvertView(Context context) {
        super(context);
        initView(context);
    }

    public AdvertView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public AdvertView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initView(context);
    }

    protected void initView(Context context){
        View view = View.inflate(context, R.layout.com_itfreer_core_ui_advanced_advertview, null);
        advPager = (ViewPager) view.findViewById(R.id.adv_pager);
        group = (LinearLayout) view.findViewById(R.id.viewGroup);

        // 这里存放的是四张广告背景
        List<View> advPics = new ArrayList<View>();

        if(images!=null && images.size()>0){
            for(int i=0;i<images.size();i++){
                ImageView img1 = new ImageView(context);
                img1.setBackgroundResource(images.get(i));
                advPics.add(img1);
            }
        }

        //对imageviews进行填充
        imageViews = new ImageView[advPics.size()];
        //小图标
        for (int i = 0; i < advPics.size(); i++) {
            imageView = new ImageView(context);
            imageView.setLayoutParams(new ViewGroup.LayoutParams(20, 20));
            imageView.setPadding(5, 5, 5, 5);
            imageViews[i] = imageView;
            if (i == 0) {
                imageViews[i].setBackgroundResource(R.drawable.point_focused);
            } else {
                imageViews[i].setBackgroundResource(R.drawable.point_unfocused);
            }
            group.addView(imageViews[i]);
        }

        advPager.setAdapter(new PageViewAdapter(advPics));
        advPager.setOnPageChangeListener(new GuidePageChangeListener());
        advPager.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                    case MotionEvent.ACTION_MOVE:
                        isContinue = false;
                        break;
                    case MotionEvent.ACTION_UP:
                        isContinue = true;
                        break;
                    default:
                        isContinue = true;
                        break;
                }
                return false;
            }
        });
        // 自动滚动
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                while (true) {
//                    if (isContinue) {
//                        viewHandler.sendEmptyMessage(what.get());
//                        whatOption();
//                    }
//                }
//            }
//        }).start();

        this.addView(view);
    }

    private void whatOption() {
        what.incrementAndGet();
        if (what.get() > imageViews.length - 1) {
            what.getAndAdd(-imageViews.length);
        }
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {

        }
    }

    private final Handler viewHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            advPager.setCurrentItem(msg.what);
            super.handleMessage(msg);
        }
    };

    private final class GuidePageChangeListener implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageScrollStateChanged(int arg0) {

        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {

        }

        @Override
        public void onPageSelected(int arg0) {
            what.getAndSet(arg0);
            for (int i = 0; i < imageViews.length; i++) {
                imageViews[arg0]
                        .setBackgroundResource(R.drawable.point_focused);
                if (arg0 != i) {
                    imageViews[i]
                            .setBackgroundResource(R.drawable.point_unfocused);
                }
            }
        }
    }
}
